class Solution {
public:
    TreeNode* _bulid(vector<int>& preorder, vector<int>& inorder,
                    int& previ, int inbegin, int inend)
    {
        //区间不存在
        if(inbegin > inend)
            return nullptr;

        TreeNode* root = new TreeNode(preorder[previ]);
        int rooti = inbegin;

        //通过中序遍历划分区间
        // [inbegin - rooti-1] rooti [rooti+1 inend]
        while(rooti <= inend)
        {
            if(preorder[previ] == inorder[rooti])
                break;
            ++rooti;
        }

        ++previ;
        root->left = _bulid(preorder,inorder,previ,inbegin,rooti - 1);
        root->right = _bulid(preorder,inorder,previ,rooti+1,inend);
        return root;


    }

    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) 
    {
        int previ = 0;
        return _bulid(preorder,inorder,previ,0,inorder.size() - 1);
    }
};

class Solution {
public:

    TreeNode* _bulid(vector<int>& inorder, vector<int>& postorder,
                        int& posend, int inbegin, int inend)
    {
        if(inbegin > inend)
            return nullptr;
        
        TreeNode* root = new TreeNode(postorder[posend]);
        int rooti = inbegin;
        while(rooti <= inend)
        {
            if(inorder[rooti] == postorder[posend])
                break;
            ++rooti;
        }
        posend--;

        root->right = _bulid(inorder,postorder,posend,rooti + 1, inend);
        root->left = _bulid(inorder,postorder,posend,inbegin, rooti - 1);
        return root;
    }

    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) 
    {
        int posend = postorder.size() - 1;
        return _bulid(inorder,postorder,posend,0,inorder.size() - 1);
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) 
    {
        stack<TreeNode*> st;
        TreeNode* cur = nullptr;
        if(root)
            cur = root;
        vector<int> res;

        //cur 或 栈不为空就继续
        while(cur || !st.empty())
        {
            //访问左节点,将左路节点依次入栈，后续依次访问左路节点的右子树
            while(cur)
            {
                res.push_back(cur->val);
                st.push(cur);
                cur = cur->left;
            }

            TreeNode* top = st.top();
            st.pop();

            //访问当前左路节点的右子树
            cur = top->right;
        }
        return res;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) 
    {
        stack<TreeNode*> st;
        TreeNode* cur = root;
        vector<int> res;

        while(cur || !st.empty())
        {
            while(cur)
            {
                st.push(cur);
                cur = cur->left;
            }

            TreeNode* top = st.top();
            st.pop();
            //访问根节点
            res.push_back(top->val);
            cur = top->right;
        }
        return res;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) 
    {
        TreeNode* prev = nullptr;//用于标识右子树上一次访问的结点
        TreeNode* cur = root;
        stack<TreeNode*> st;
        vector<int> res;

        while(cur || !st.empty())
        {
            //左路子树入栈
            while(cur)
            {
                st.push(cur);
                cur = cur->left;
            }
            TreeNode* top = st.top();

            //左右都已经访问过了才访问根
            //由于后序遍历的特殊性，我们需要确定了右节点为空或者右节点已经访问过了才能访问根
            if(top->right == nullptr || top->right == prev)
            {
                prev = top;
                res.push_back(top->val);
                st.pop();
            }
            else
            {
                cur = top->right; 
            }
        }
        return res;
    }
};
